package com.wandousoushu.jiusen.booksource.model

import androidx.lifecycle.MutableLiveData
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.booksource.data.entities.Book
import com.wandousoushu.jiusen.booksource.data.entities.SearchBook
import com.wandousoushu.jiusen.web.http.MainHttpUtils
import java.util.ArrayList

class SearchBookManager {

    private val searchBookList: MutableList<SearchBook> = ArrayList()
    var historyKeyList = MutableLiveData<List<String>>()
    private val mHistoryKeys: MutableList<String> = ArrayList()

    companion object {
        private val instance = SearchBookManager()
        fun GetInstance(): SearchBookManager {
            return instance
        }
    }

    fun AddSearckBook(searchBook: SearchBook) {
        searchBookList.add(searchBook)
    }

    fun AddSearckBooks(searchBook: List<SearchBook>) {
        searchBookList.addAll(searchBook)
    }

    fun getSearchBookList(): List<SearchBook> {
        return searchBookList
    }

    fun getFirstSearchBookByNameAuthor(name: String, author: String): SearchBook? {
        for (book in searchBookList) {
            if (book.name == name && book.author == author) {
                return book
            }
        }
        return null
    }

    fun getSearchBookByUrl(url: String): Book? {
        for (book in searchBookList) {
            if (book.bookUrl == url) {
                val mbook1 = Book()
                mbook1.author = book.author
                mbook1.coverUrl = book.coverUrl
                mbook1.intro = book.intro
                mbook1.name = book.name
                mbook1.origin = book.origin
                mbook1.originName = book.originName
                mbook1.bookUrl = book.bookUrl
                mbook1.latestChapterTitle = book.latestChapterTitle
                mbook1.tocUrl = book.tocUrl
                return mbook1
            }
        }
        return null
    }

    fun getSearchBookByNameAuthor(name: String, author: String): List<SearchBook>? {
        val newSearchBookList: MutableList<SearchBook> = ArrayList()
        for (book in searchBookList) {
            if (book.name == name && book.author == author) {
                newSearchBookList.add(book)
            }
        }
        return newSearchBookList
    }

    fun getChangeSourceSearch(name: String, author: String, key: String): List<SearchBook>? {
        val newSearchBookList: MutableList<SearchBook> = ArrayList()
        for (book in searchBookList) {
            if (book.name == name && book.author == author) {
                newSearchBookList.add(book)
            }
        }
        return newSearchBookList
    }

    fun AddHistoryKeys(historyKeys: List<String>) {
        mHistoryKeys.clear()
        mHistoryKeys.addAll(historyKeys)
        historyKeyList.postValue(mHistoryKeys)
    }

    fun AddHistoryKey(key: String) {
        if (!mHistoryKeys.contains(key)) {
            mHistoryKeys.add(key)
            historyKeyList.postValue(mHistoryKeys)
        }
        if (MyApplication.instance.isLogin){
            MainHttpUtils.getInstance().AddSearchKey(key, MyApplication.instance.token)
        }
    }

    fun RomeveHistoryKey(key: String?) {
        mHistoryKeys.remove(key)
        historyKeyList.postValue(mHistoryKeys)
        if (MyApplication.instance.isLogin){
            MainHttpUtils.getInstance().RemoveSearchKey(key, MyApplication.instance.token)
        }
    }

    fun RomeveHistoryKeys() {
        mHistoryKeys.clear()
        historyKeyList.postValue(mHistoryKeys)
        if(MyApplication.instance.isLogin){
            MainHttpUtils.getInstance().RemoveSearchKeys(MyApplication.instance.token)
        }
    }

    fun clear(){
        mHistoryKeys.clear()
        historyKeyList.postValue(mHistoryKeys)
        searchBookList.clear()
    }
}